provider "alicloud" {
  profile = "default"
  version = "1.144.0"
}

resource "alicloud_ram_user" "user" {
  name         = "user_test"
  display_name = "user_display_name"
  mobile       = "86-18688888888"
  email        = "hello.uuu@aaa.com"
  comments     = "yoyoyo"
  force        = true
}

resource "alicloud_ram_access_key" "ak" {
  user_name   = alicloud_ram_user.user.name
  secret_file = "/tmp/ak.txt"
}

resource "alicloud_ram_role" "role" {
  name     = "testrole"
  document = <<EOF
  {
    "Statement": [
      {
        "Action": "sts:AssumeRole",
        "Effect": "Allow",
        "Principal": {
          "Service": [
            "ecs.aliyuncs.com"
          ]
        }
      }
    ],
    "Version": "1"
  }

EOF

  description = "this is a test"
  force       = true
}

resource "alicloud_ram_policy" "policy" {
  name        = "policyName"
  document    = <<EOF
  {
    "Statement": [
      {
        "Action": [
          "oss:ListObjects",
          "oss:GetObject"
        ],
        "Effect": "Allow",
        "Resource": [
          "acs:oss:*:*:mybucket",
          "acs:oss:*:*:mybucket/*"
        ]
      }
    ],
    "Version": "1"
  }
  EOF
  description = "this is a policy test"
  force       = true
}

resource "alicloud_ram_user_policy_attachment" "attach" {
  policy_name = alicloud_ram_policy.policy.name
  policy_type = alicloud_ram_policy.policy.type
  user_name   = alicloud_ram_user.user.name
}

resource "alicloud_vpc" "main" {
  vpc_name = "ebike"
  cidr_block = "192.168.0.0/16"
}

resource "alicloud_vswitch" "vs-1" {
  vswitch_name = "vsw-h"
  vpc_id            = alicloud_vpc.main.id
  cidr_block        = "192.168.1.0/24"
  zone_id = "cn-hangzhou-h"
  depends_on = [alicloud_vpc.main]
}

resource "alicloud_vswitch" "vs-2" {
  vswitch_name = "vsw-j"
  vpc_id            = alicloud_vpc.main.id
  cidr_block        = "192.168.2.0/24"
  zone_id = "cn-hangzhou-j"
  depends_on = [alicloud_vpc.main]
}

resource "alicloud_eip_address" "default" {
  address_name         = "ebike"
  isp                  = "BGP"
  internet_charge_type = "PayByTraffic"
  payment_type         = "PayAsYouGo"
  bandwidth            = "200"
}

resource "alicloud_slb_load_balancer" "default" {
  load_balancer_name = "slb"
  load_balancer_spec = "slb.s2.small"
  vswitch_id = alicloud_vswitch.vs-1.id
  tags = {
    env = "prod"
  }
}

resource "alicloud_eip_association" "eip_asso_slb" {
  allocation_id = alicloud_eip_address.default.id
  instance_id   = alicloud_slb_load_balancer.default.id
  depends_on = [alicloud_slb_load_balancer.default]
}

resource "alicloud_slb_listener" "tcp" {
  load_balancer_id          = alicloud_slb_load_balancer.default.id
  backend_port              = "22"
  frontend_port             = "22"
  protocol                  = "tcp"
  bandwidth                 = "10"
  health_check_type         = "tcp"
  persistence_timeout       = 3600
  healthy_threshold         = 8
  unhealthy_threshold       = 8
  health_check_timeout      = 8
  health_check_interval     = 5
  health_check_http_code    = "http_2xx"
  health_check_connect_port = 20
  health_check_uri          = "/console"
  acl_status                = "on"
  acl_type                  = "black"
  acl_id                    = alicloud_slb_acl.acl.id
  established_timeout       = 600
  depends_on = [alicloud_slb_acl.acl]
}

resource "alicloud_slb_acl" "acl" {
  name       = "黑名单"
  ip_version = "ipv4"

  entry_list {
    entry   = "10.10.10.0/24"
    comment = "first"
  }
  entry_list {
    entry   = "168.10.10.0/24"
    comment = "second"
  }
  entry_list {
    entry   = "172.10.10.0/24"
    comment = "third"
  }
}

resource "alicloud_security_group" "default" {
  name = "default"
  vpc_id = alicloud_vpc.main.id
}

resource "alicloud_security_group_rule" "allow_all_tcp" {
  type              = "ingress"
  ip_protocol       = "tcp"
  nic_type          = "intranet"
  policy            = "accept"
  port_range        = "1/65535"
  priority          = 1
  security_group_id = alicloud_security_group.default.id
  cidr_ip           = "0.0.0.0/0"
}

resource "alicloud_rds_parameter_group" "default" {
  engine         = "mysql"
  engine_version = "5.7"
  param_detail {
    param_name  = "back_log"
    param_value = "4000"
  }
  param_detail {
    param_name  = "wait_timeout"
    param_value = "86460"
  }
  parameter_group_desc = "test"
  parameter_group_name = "test1234"
}

# rds
variable "creation" {
  default = "Rds"
}

data "alicloud_zones" "example" {
  available_resource_creation = var.creation
}

output "rds_id" {
  description = "The ID of the VPC"
  value       = data.alicloud_zones.example.zones.0.id
}

resource "alicloud_db_instance" "example" {
  engine               = "MySQL"
  engine_version       = "8.0"
  instance_type        = "rds.mysql.s2.large"
  instance_storage     = "30"
  instance_charge_type = "Postpaid"
  instance_name        = "ebike"
  vswitch_id           = alicloud_vswitch.vs-1.id
  zone_id              = "cn-hangzhou-h"
  monitoring_period    = "60"
  security_ips         = ["127.0.0.1", "10.168.1.12", "100.69.7.112"]
}

resource "alicloud_db_connection" "foo" {
  instance_id       = alicloud_db_instance.example.id
  connection_prefix = "testabc"
}

resource "alicloud_db_database" "default" {
  count = 1
  instance_id = alicloud_db_instance.example.id
  name        = "tftestdatabase"
}

resource "alicloud_db_account" "account" {
  instance_id = alicloud_db_instance.example.id
  name        = "tftestnormal"
  password    = "Test12345"
}

resource "alicloud_db_account_privilege" "privilege" {
  instance_id  = alicloud_db_instance.example.id
  account_name = alicloud_db_account.account.name
  privilege    = "ReadOnly"
  db_names     = alicloud_db_database.default.*.name
}

output "vpc_id" {
  description = "The ID of the VPC"
  value       = alicloud_vpc.main.id
}